<?php
use \wechat\OscshopWechat;
use \think\Db;

if (!function_exists('wechat')) {
    /**
     * 助手函数
     */
    function wechat()
    {
    	return OscshopWechat::getInstance([
    	'appid'=>config('appid'),
    	'appsecret'=>config('appsecret'),
    	'token'=>config('token'),
    	'encodingaeskey'=>config('encodingaeskey')]);		
    }
}

/**
 * 查询交易所主账户
 * @param $username
 * @param $exchange_id
 * @return mixed
 */
function get_trade_id($username,$exchange_id){
	$data = Db::name('exchange_trade_acc')->where(['username'=>$username,'exchange_id'=>$exchange_id])->find();
	return $data['trade_id'];
}

/**
 * 查询交易所副账户
 * @param $username
 * @param $exchange_id
 * @return mixed
 */
function get_trade_acc_id($username,$exchange_id){
	$data = Db::name('exchange_trade_acc')->where(['username'=>$username,'exchange_id'=>$exchange_id])->find();
	return $data['sub_trade_id'];
}

/**
 * 根据用户名查询用户真实姓名
 * @param $username
 * @return mixed
 */
function get_member_idcardname($username){
	$data = Db::name('member')->where('username',$username)->field('idcardname')->find();
	return $data['idcardname'];
}

/**
 * 通过admin_id查询管理员账户
 * @param $admin_id
 * @return array|false|mixed|PDOStatement|string|\think\Model
 */
function get_admin_user_name($admin_id){
	$res = Db::name('admin')->where(['admin_id'=>$admin_id])->field('user_name')->find();
	return $res['user_name'];
}

/**
 * 查询会员等级
 * @param $username
 * @return string
 */
function get_vip_grade($username){
	$res = Db::name('member')->where(['username'=>$username])->find();
	if($res['user_state'] == 2){
		return '代理';
	}
	if(0 <= $res['share_count'] and $res['share_count'] < 2){
		$name = 'VIP会员';
	}elseif(2 <= $res['share_count'] and $res['share_count'] < 5){
		$name = '一级会员';
	}elseif(5 <= $res['share_count'] and $res['share_count'] < 8){
		$name = '二级会员';
	}else{
		$name = '三级会员';
	}
	return $name;
}

/**获取交易所名称
 * @param $trade_id
 * @return mixed
 */
function get_trade_name($trade_id){
	$res = Db::name('exchange_house')->where('id',$trade_id)->find();
	return $res['name'];
}

/**
 * 获取交易品种名称
 * @param $variety_id
 * @return mixed
 */
function get_variety_name($variety_id){
	$res = Db::name('exchage_variety')->where('id',$variety_id)->find();
	return $res['name'];
}

/**
 * 获取交易品种品种号
 * @param $variety_id
 * @return mixed
 */
function get_variety_id($variety_id){
	$res = Db::name('exchage_variety')->where('id',$variety_id)->find();
	return $res['variety_id'];
}

/**
 * 获取周期单位
 * @param $type
 * @return string
 */
function get_contract_time_type($type){
	if($type == 1){
		return '天';
	}elseif($type == 2){
		return '月';
	}elseif($type == 3){
		return '年';
	}
}

/**
 * 获取合约名称
 * @param $contract_id
 * @return mixed
 */
function get_contract_id_name($contract_id){
	$res = Db::name('contract_info')->where('id',$contract_id)->find();
	return $res['name'];
}

/**
 * 获取合约时间长度
 * @param $time_id
 * @return mixed
 */
function get_contract_time_id_num($time_id){
	$res = Db::name('contract_time_config')->where('id',$time_id)->find();
	return $res['num'];
}

/**
 * 获取合约时间单位
 * @param $time_id
 * @return mixed
 */
function get_contract_time_id_type($time_id){
	$res = Db::name('contract_time_config')->where('id',$time_id)->find();
	return $res['type'];
}

/**
 * 根据周期id获取周期名称
 * @param $time_id
 * @return string
 */
function get_time_id_name($time_id){

	$res = Db::name('contract_time_config')->where('id',$time_id)->find();

	if($res['type'] == 1){
		$type = '天';
	}elseif($res['type'] == 2){
		$type = '月';
	}elseif($res['type'] == 3){
		$type = '年';
	}else{
		$type = '';
	}
	return $res['num'].$type;
}

/**
 * 获取价格区间
 * @param $money_id
 * @return string
 */
function get_contract_money_id_price($money_id){
	$res = Db::name('contract_money_config')->where('id',$money_id)->find();
	return $res['min_price'].'—'.$res['max_price'];
}

/**
 * 获取用户名
 * @param $contract_id
 * @return mixed
 */
function get_contract_b1_id_username($contract_id){
	$res = Db::name('contract_b1')->where('id',$contract_id)->find();
	return $res['username'];
}

/**
 * 获取用户名
 * @param $contract_id
 * @return mixed
 */
function get_contract_b2_id_username($contract_id){
	$res = Db::name('contract_b2')->where('id',$contract_id)->find();
	return $res['username'];
}

/**
 * 根据交易品种获取最新交易价格
 * @param $variety_id
 * @return mixed
 */
function get_value_max($variety_id){
	$value_max = Db::name('value_config')->where(['variety_id'=>$variety_id])->order('dt desc')->find();
	return $value_max['rate'];
}

/**
 * 根据交易品种获取发行价
 * @param $variety_id
 * @return mixed
 */
function get_contract_issue($variety_id){
	$issue = Db::name('contract_issue')->where(['variety_id'=>$variety_id])->find();
	return $issue['issue'];
}

/**
 * 模拟post进行url请求
 * @param string $url  地址
 * @param string $param  参数
 * @return mixed
 */
function request_post($url = '', $param = '') {
	if (empty($url) || empty($param)) {
		return false;
	}

	$postUrl = $url;
	$curlPost = $param;
	$ch = curl_init();//初始化curl
	curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
	curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
	curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
	curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
	$data = curl_exec($ch);//运行curl
	curl_close($ch);

	return $data;
}

/**
 * 获取自由买卖的买入凭证
 * @param $contract_id
 * @return mixed
 */
function get_bi_img($contract_id){
	$res = Db::name('contract_b1_img')->where('contract_id',$contract_id)->where('seq_id',2)->find();
	return $res['img_path'];
}

/**
 * 获取自由买卖的买入凭证
 * @param $contract_id
 * @return mixed
 */
function get_bi_image($contract_id){
	$res = Db::name('contract_b1_img')->where('contract_id',$contract_id)->where('seq_id',1)->find();
	return $res['img_path'];
}

/**
 * 获取自由买卖的合约数
 * @param $username
 * @return mixed
 */
function is_get_username_b1($username){
	$res = Db::name('contract_b1')->where('username',$username)->count();

	return $res;
}


/**
 * 计算要求卖出单价
 * @param $contract_id
 * @return mixed
 */
function get_out_unit_price($contract_id){

	$contract = Db::name('contract_b1')->where('id',$contract_id)->find();

	if($contract['b_num']){
		$rate = Db::name('contract_rate_config')->where('id',$contract['rate_id'])->find();
		//查询出合约开始时间前一天的收盘价
		$day = date('Y-m-d',strtotime('-1 day',strtotime($contract['begin_day'])));
		$value = Db::name('value_config')->where(['exchange_id'=>$contract['d_exchange'],'variety_id'=>$contract['d_exchange_variety']])->where('dt',$day)->find();
		// print_r($value);exit;
		$unit_price =round(($contract['b_price'] + ($contract['d_num'] * $value['rate']) + ($contract['b_price'] * $rate['rate']))/$contract['b_num'],2);
		return $unit_price;
	}else{
		return '';
	}
}

/**
 * 根据会员等级给出释放比例
 * @param $state
 * @return float|int
 */
function get_user_state_rate($state){
	$rate = 0;
	if($state == 1){
		$rate = 0.004;
	}elseif($state == 2){
		$rate = 0.006;
	}elseif($state == 3){
		$rate = 0.007;
	}elseif($state == 4){
		$rate = 0.008;
	}elseif($state == 5){
		$rate = 0.009;
	}

	return $rate;
}

/**
 * 获取用户推荐人
 * @param $username
 * @return mixed
 */
function get_sharemobile($username){

	$user = Db::name('member')->where('username',$username)->find();

	return $user['sharemobile'];
}

function get_free_img($contract_id){
	$res = Db::name('free_contract_img')->where('free_id',$contract_id)->where('seq_id',1)->find();
	return $res['img_path'];
}

